﻿using AutoMapper;
using BackData;
using Domain;
using Infratructure;
using MediatR;
using Rabbit.API.Application.Command.Kill;

namespace Rabbit.API.Application.Handler.Kill
{
    public class KillUpdateHandler:IRequestHandler<KillUpdateCommand,APIResult<int>>
    {
        private readonly IRepository<KillModel> killway;
        private readonly IMapper mapper;
        private readonly ILogger<KillGetHandler> log;

        public KillUpdateHandler(IRepository<KillModel> killway, IMapper mapper, ILogger<KillGetHandler> log)
        {
            this.killway = killway;
            this.mapper = mapper;
            this.log = log;
        }
        /// <summary>
        /// 屠宰信息修改
        /// </summary>
        /// <param name="request">参数</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>影响行数</returns>
        public async Task<APIResult<int>> Handle(KillUpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> res = new APIResult<int>();
            try
            {
                //var value = killway.GetValue(request.Id);
                var value = mapper.Map<KillModel>(request);
                var result = await killway.Updateway(value);
                if (result > 0)
                {
                    log.LogInformation($"屠宰信息修改成功");
                    res.Message = "屠宰信息修改成功";
                    res.Num = StateCode.OK;
                    res.Data = result;

                }
                else
                {
                    res.Message = "屠宰信息修改失败";
                    res.Num = StateCode.Fail;
                }
                return res;

            }
            catch (Exception ex)
            {
                log.LogError($"屠宰信息修改发生异常{ex.Message}");
                throw;
            }
        }
    }
}
